package cn.com.headfree.evaluation.export.policy;

import cn.com.headfree.vo.ThreeYearTable;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;
import java.util.List;

public class ThreeYearExpertPolicy extends DynamicTableRenderPolicy {

    int threeYearRow = 0;

    @Override
    public void render(XWPFTable table, Object data) {
        setTableBorder(table);
        if (null == data) return;
        ThreeYearTable detailData = (ThreeYearTable) data;
        List<RowRenderData> threeYear = detailData.getThreeYears();
        if (null != threeYear && threeYear.size() > 0) {
            table.removeRow(threeYearRow);
            // 循环插入行
            for (int i = 0; i < threeYear.size(); i++) {
                if(i > 0){
                    table.removeRow(threeYearRow+i+1);
                }
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(threeYearRow);
                insertNewTableRow.setHeight(800);//设置行高
                for (int j = 0; j < 3; j++){
                    XWPFTableCell cell = insertNewTableRow.createCell();
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                    setCellBorder(cell);
                }
                MiniTableRenderPolicy.Helper.renderRow(table, threeYearRow, threeYear.get(i));
            }
        }
    }

    public void setTableBorder(XWPFTable table){
        CTTblBorders borders = table.getCTTbl().getTblPr().addNewTblBorders();
        CTBorder hBorder = borders.addNewInsideH();
        hBorder.setVal(STBorder.Enum.forString("single"));
        hBorder.setSz(new BigInteger("1"));
        hBorder.setColor("000000");

        CTBorder vBorder = borders.addNewInsideV();
        vBorder.setVal(STBorder.Enum.forString("single"));
        vBorder.setSz(new BigInteger("1"));
        vBorder.setColor("000000");

        CTBorder lBorder = borders.addNewLeft();
        lBorder.setVal(STBorder.Enum.forString("single"));
        lBorder.setSz(new BigInteger("1"));
        lBorder.setColor("000000");

        CTBorder rBorder = borders.addNewRight();
        rBorder.setVal(STBorder.Enum.forString("single"));
        rBorder.setSz(new BigInteger("1"));
        rBorder.setColor("000000");

        CTBorder tBorder = borders.addNewTop();
        tBorder.setVal(STBorder.Enum.forString("single"));
        tBorder.setSz(new BigInteger("1"));
        tBorder.setColor("000000");

        CTBorder bBorder = borders.addNewBottom();
        bBorder.setVal(STBorder.Enum.forString("single"));
        bBorder.setSz(new BigInteger("1"));
        bBorder.setColor("000000");
    }

    //设置边框
    public void setCellBorder(XWPFTableCell cell){
        CTTcBorders cTTcBorders = cell.getCTTc().getTcPr().addNewTcBorders();
        CTBorder clBorder = cTTcBorders.addNewLeft();
        clBorder.setVal(STBorder.Enum.forString("single"));
        clBorder.setShadow(STOnOff.ON);
        CTBorder crBorder = cTTcBorders.addNewRight();
        crBorder.setVal(STBorder.Enum.forString("single"));
        crBorder.setShadow(STOnOff.ON);
        CTBorder cbBorder = cTTcBorders.addNewBottom();
        cbBorder.setVal(STBorder.Enum.forString("single"));
        cbBorder.setShadow(STOnOff.ON);
        CTBorder ctBorder = cTTcBorders.addNewTop();
        ctBorder.setVal(STBorder.Enum.forString("single"));
        ctBorder.setShadow(STOnOff.ON);
    }
}
